package mrusanov.player.psychology

import mrusanov.player.Player
import java.util.Date
import util.Random

object Conflict {
  def newConflicts(players:Vector[Player]):List[Conflict] = {
    val conflicts = for (i <- 0 until players.size; j <- (i + 1) until players.size) yield {
      val first = players(i)
      val second = players(j)
      val chanceOfConflict = first.psychology.chanceOfConflict(second.psychology)
      val random = Random.nextInt(100)
      if (random < chanceOfConflict) {
        Some(new Conflict(first, second))
      } else {
        None
      }
    }

    conflicts.flatten.toList
  }
}

class Conflict(val first:Player, val second:Player) {

  private def players = Set(first, second)

  def startConflict() {
    //if they were in friendship, break friendship
    first.psychology.friends -= second
    second.psychology.friends -= first

    // set them as enemies
    first.psychology.enemies += second
    second.psychology.enemies += first
  }

  override def hashCode = first.hashCode() + second.hashCode()

  override def equals(other:Any) = other match {
    case conf:Conflict => conf.players == this.players
    case _ => false
  }
}
